﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using CommonTools;
using log4net;
using Sunny.UI;
using StationDemo.FrameUI;
namespace StationDemo
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
       
        static void Main()
        {
            if (Mutex.TryOpenExisting("StationDemo", out Mutex mutex) == false)
            {
                mutex = new Mutex(true, "StationDemo");
            }
            if (mutex.WaitOne(1) == false)
            {
                UIMessageBox.ShowError("请勿多开");
                //UIMessageDialog.ShowMessageDialog(null, "请勿多开","提示", false, UIStyle.Red);
                //MessageBox.Show("请勿同时打开多个程序", "Warn", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                mutex.Dispose();
                return;
            }
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            log4net.Config.XmlConfigurator.Configure();
            ILog log = LogManager.GetLogger("Program");
            try
            {
                //处理未捕获的异常
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                //处理UI线程异常
                Application.ThreadException += Application_ThreadException;
                //处理非UI线程异常
                AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
                string strPath = Path.GetFullPath(".");

                string exePath = Path.GetFullPath(".") + "\\HYEquipment.exe";
                FileInfo fi = new FileInfo(exePath);

          

                log.Info("创建时间：" + fi.CreationTime.ToString() + "写入文件的时间" + fi.LastWriteTime + "访问的时间" + fi.LastAccessTime);
                Console.Write("创建时间：" + fi.CreationTime.ToString() + "写入文件的时间" + fi.LastWriteTime + "访问的时间" + fi.LastAccessTime);
                Application.Run(new Form1());
                _ExitApp = true;
            }
            catch (Exception e)
            {
                log.Info($"程序异常：{e.Message}，###+ {e.StackTrace}");
                UIMessageBox.ShowError($"程序异常：{e.Message}");
            } 
            

         
        }
        static bool _ExitApp = false;
        /// <summary>
        /// 处理未处理的UI异常
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            ILog log = LogManager.GetLogger("Program");
            Exception ex = (Exception)e.ExceptionObject;
            log.Info($"程序异常 异常类型[UnhandledException]：{ex.Message}，###+ {ex.StackTrace}");
            if (ex.Message.Contains("停止") || ex.Message.Contains("正在中止线程") || ex.Message.Contains("Thread was being aborted"))
            {
                while (true)
                {
                    if (_ExitApp)
                    {
                        log.Info($"程序退出");
                        System.Environment.Exit(0);
                        return;
                    }
                    System.Threading.Thread.Sleep(200 * 10000);
                };
             //   UIMessageBox.ShowError($"sssss程序异常 程序将会退出：{ex.Message}\r\n {ex.StackTrace}");
             //  
                return;
            }
            else
            {
                StationMgr.GetInstance().Stop();
                DoWhile.StopCirculate();
                UIMessageBox.ShowError($"程序异常 异常类型[UnhandledException]：{ex.Message}\r\n {ex.StackTrace}");

            }
               
        }
        /// <summary>
        /// 处理未处理的线程异常
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            ILog log = LogManager.GetLogger("Program");
            log.Info($"程序异常 异常类型[ThreadException]：{e.Exception.Message}，###+ {e.Exception.StackTrace}");
            log.Info($"程序异常：{e.Exception}，###+ {e.Exception.StackTrace}");
            if (e.Exception.Message.Contains("停止") || e.Exception.Message.Contains("正在中止线程") || e.Exception.Message.Contains("Thread was being aborted") /*|| e.Exception.GetType() == typeof(AutoRunStopException)*/)
            {
                return;
            }
            else
            {
                StationMgr.GetInstance().Stop();
                DoWhile.StopCirculate();
                UIMessageBox.ShowError($"线程异常 异常类型[ThreadException]：{e.Exception.Message} \r\n {e.Exception.StackTrace}");
            }
               
        }
    }


}
